<?php
/**
 * 图片上传
 * @文件名称: Upload.php
 * @author: jawei
 * @Email: gaozhiwei@etcp.cn
 * @Date: 2017-06-06
 * @Copyright: 2017 悦畅科技有限公司. All rights reserved.
 * 注意：本内容仅限于悦畅科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
namespace app\components;

use Yii;
use app\services\jdb\UPYun;
use app\base\BaseException;

class Upload
{
    private static $_filterType = [
        'txt', 'log', 'jpg', 'png', 'jpeg', 'gif'
    ];

    /**
     * 上传文件, 返回文件名: upload_file
     */
    public static function file()
    {
        $file = self::base();
        $name = date('YmdHis', time()) . $file['name'];
        $uploadFileStoreUrl = Yii::$app->params['upload']['fileStoreUrl'] . $name;
        $result = move_uploaded_file($file['tmp_name'], $uploadFileStoreUrl);
        if ($result != true) {
            throw new BaseException('move upload file fail', 50049);
        }

        return $name;
    }

    /**
     *
     *
     * @return string
     * @throws BaseException
     */
    public static function fileForOSS()
    {
        $file = self::base();
        $name = date('YmdHis', time()) . $file['name'];
        $uploadFileStoreUrl = Yii::$app->params['upload']['fileStoreUrl'] . $name;
        $result = move_uploaded_file($file['tmp_name'], $uploadFileStoreUrl);
        if ($result != true) {
            throw new BaseException('move upload file fail', 5004865);
        }

        $name = DmpOss::getObjectNameByFileUrl($uploadFileStoreUrl);

        $result = DmpOss::putObject(DmpOss::getInstance(), $uploadFileStoreUrl, $name);
        if(!$result){
            throw new BaseException('文件上传到对象存储失败.', 5004256);
        }

        return $name;
    }

    /**
     * 将文件上传到对象存储, 返回对象存储的对象名
     *
     * @return string
     * @throws BaseException
     */
    public static function fileForOssV2()
    {
        $file = self::base();
        $objName = DmpOss::getObjectNameByFileUrl($file['name']);

        $result = DmpOss::putObject(DmpOss::getInstance(), $file['name'], $objName);
        if(!$result){
            throw new BaseException('文件上传到对象存储失败.', 5004256);
        }

        return $objName;
    }

    /**
     * 上传图片, 返回文件名
     */
    public static function image()
    {
        $file = self::base();
        return UPYun::upload($file);
    }

    public static function getUploadFile()
    {
        return self::base();
    }

    private static function base()
    {
        DmpUtil::initPHPUploadConfig();

        $tmpFile = [];
        foreach ($_FILES as $k => $v) {
            $tmpFile = $v;
            break;
        }

        if (empty($tmpFile)) {
            throw new BaseException('empty tmp file: ' . json_encode($tmpFile), 40370);
        }

        $file = [
            'name' => urlencode($tmpFile['name']),
            'type' => $tmpFile['type'],
            'size' => $tmpFile['size'],
            'tmp_name' => $tmpFile['tmp_name'],
            'error' => $tmpFile['error'],
        ];

        if (!DmpUtil::issetEmptyCheckBool($file['name'])) {
            throw new BaseException('upload file name is empty', 40333);
        }

        switch ($file['error']) {
            case UPLOAD_ERR_OK:
                break;
            case UPLOAD_ERR_INI_SIZE:
                throw new BaseException('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。', 50068);
            case UPLOAD_ERR_FORM_SIZE:
                throw new BaseException('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。', 50071);
            case UPLOAD_ERR_PARTIAL:
                throw new BaseException('文件只有部分被上传。', 50074);
            case UPLOAD_ERR_NO_FILE:
                throw new BaseException('没有文件被上传。', 50077);
            case UPLOAD_ERR_NO_TMP_DIR:
                throw new BaseException('找不到临时文件夹。', 50076);
            case UPLOAD_ERR_CANT_WRITE:
                throw new BaseException('文件写入失败。', 50078);
        }

        $type = substr($file['name'], strrpos($file['name'], '.') + 1);

        if (!in_array(strtolower($type), self::$_filterType)) {
            throw new BaseException('file type is invalid: ' . $type, 40339);
        }

        return $file;
    }
}
